//
//  LZTComposeTextView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/17.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit

class LZTComposeTextView: UITextView {
    
    //占位文字的大小 动态设定-VC传值
    override var font: UIFont? {
        didSet{
            
            //设置 占位文字的font
            placeHolderLab.font = font
        }
    }
    
    //占位文字 直接让外界可以赋值
    var placeHolder: String? {
        didSet{
            placeHolderLab.text = placeHolder
        }
    }
    

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置试图
    func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(placeHolderLab)
        
        //添加约束
        placeHolderLab.snp_makeConstraints { (make) in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENW - 10)
        }
        
        //监听系统通知 查看textView中文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LZTComposeTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    func textViewTextDidChange() {
        placeHolderLab.hidden = self.hasText()
    }
    
    
    //懒加载 label显示 占位文字
    private lazy var placeHolderLab: UILabel = {
        
        let lab = UILabel()
        lab.textColor = UIColor.redColor()
        lab.numberOfLines = 0
        return lab
        
    }()
    
    //析构函数 销毁通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
